/**数据验证*/
var CommunityValidator = function() {  
    var handleSubmit = function() { 
        $('.form-horizontal').validate({  
            errorElement : 'span',  
            errorClass : 'help-block',  
            focusInvalid : false,  
            rules : {  
            	"cnum" : {  
                    required : true,
                    digits:true
                },  
                "unum" : {  
                    required : true,
                    digits:true
                },
                "lnum"  :{
                	required : true,
                	digits:true
                },
                "rnum"  :{
                	required : true,
                	digits:true
                }
            },  
            messages : {  
            	"cnum" : {  
            		required : "请输入楼栋数",
            		digits:"请输入整数（例如：1，2，3）"
                },  
                "unum" : {  
                	required : "请输入每栋楼的单元数",
                	digits:"请输入整数（例如：1，2，3）"
                },
                "lnum"  :{
                	required : "请输入每个单元的楼层数",
                	digits:"请输入整数（例如：1，2，3）"
                },
                "rnum"  :{
                	required : "请输入每层户数",
                	digits:"请输入整数（例如：1，2，3）"
                }
            },  
  
            highlight : function(element) { 
                $(element).closest('.control-group').addClass('error');  
            },  
  
            success : function(label) { 
                label.closest('.control-group').removeClass('error');  
                label.remove();
            },  
  
            errorPlacement : function(error, element) {  
            	element.parent('div').append(error);  
            },  
  
            submitHandler : function(form) {
                form.submit();
            }  
        });  
  
        $('.form-horizontal input').keypress(function(e) {  
            if (e.which == 13) {  
                if ($('.form-horizontal').validate().form()) {  
                    $('.form-horizontal').submit();  
                }  
                return false;  
            }  
        });  
    }  
    return {  
        init : function() { 
            handleSubmit(); 
        }  
    };  
  
}();
var setting = {
		view: {
			addHoverDom: addHoverDom,
			removeHoverDom: removeHoverDom,
			selectedMulti: false
		},
		edit: {
			enable: true,
			editNameSelectAll: true
		},
		data: {
			simpleData: {
				enable: true
			}
		}
};
var newCount = 1;
function addHoverDom(treeId, treeNode) {
	var sObj = $("#" + treeNode.tId + "_span");
	if (treeNode.editNameFlag || $("#addBtn_"+treeNode.id).length>0) return;
	var addStr = "<span class='button add' id='addBtn_" + treeNode.id
		+ "' title='add node' onfocus='this.blur();'></span>";
	sObj.after(addStr);
	var btn = $("#addBtn_"+treeNode.id);
	if (btn) btn.bind("click", function(){
		var zTree = $.fn.zTree.getZTreeObj("menup");
		zTree.addNodes(treeNode, {id:("T"+100 + newCount), pId:treeNode.id, name:"节点名称" + (newCount++)});
		return false;
	});
};
function removeHoverDom(treeId, treeNode) {
	$("#addBtn_"+treeNode.id).unbind().remove();
};
function generator(){
	$('#myModal').modal('show');
	var cnum=$("#cnum").val();
	var unum=$("#unum").val();
	var lnum=$("#lnum").val();
	var rnum=$("#rnum").val();
	var cname=$("#cname").val();
	var cid=$("#cid").val();
	var crcode=$("#crcode").val();
	var urcode=$("#urcode").val();
	var lrcode=$("#lrcode").val();
	var rrcode=$("#rrcode").val();
	$.post("../base/generator_Community.action",{"community.cnum":cnum,"community.unum":unum,"community.lnum":lnum,"community.rnum":rnum,
		"community.cname":cname,"community.cid":cid,"community.crule.rcode":crcode,"community.urule.rcode":urcode,"community.lrule.rcode":lrcode,"community.rrule.rcode":rrcode}, function(result) {
			if(result.msg==null || result.msg=="undefined"){
				$.fn.zTree.init($("#menup"), setting, result.community);
				$('#myModal').modal('hide');
			}else{
				$('#myModal').modal('hide');
				$("#alertinfo").html(result.msg);
				$('#myinfo').modal('show');
			}
	});
	
};


/**保存数据*/
function saveTHCommunity(){
	$('#myModal').modal('show');
	var cid=$("#cid").val();
	var cnum=$("#cnum").val();
	var unum=$("#unum").val();
	var lnum=$("#lnum").val();
	var rnum=$("#rnum").val();
	var crcode=$("#crcode").val();
	var urcode=$("#urcode").val();
	var lrcode=$("#lrcode").val();
	var rrcode=$("#rrcode").val();
	var zTree = $.fn.zTree.getZTreeObj("menup");
	if(zTree==null || zTree=='undefined'){
		alert('请首先点击生成套户资料按钮.');
		$('#myModal').modal('hide');
		return;
	}
	var nodes=zTree.getNodes();
	
	
	$.post("../base/saveTH_Community.action",{"community.cid":cid,"community.cnum":cnum,"community.unum":unum,"community.lnum":lnum,"community.rnum":rnum,
		"community.crule.rcode":crcode,"community.urule.rcode":urcode,"community.lrule.rcode":lrcode,"community.rrule.rcode":rrcode,"msg":JSON.stringify(nodes)}, function(result) {
		if(result.msg=="succ"){
			$('#myModal').modal('hide');
			$("#alertinfo").html("小区维护成功");
			$('#myinfo').modal('show');
			$("#loginid").attr("disabled",true);
		}else{
			$('#myModal').modal('hide');
			$("#alertinfo").html(result.msg);
			$('#myinfo').modal('show');
		}
	});
};

$(document).ready(function(){
	CommunityValidator.init();
	/*加载已生成的客套资料*/
	var cid=$("#cid").val();
	$('#myModal').modal('show');
	$.post("../base/show_Community.action",{"community.cid":cid}, function(result) {
			if(result.msg==null || result.msg=="undefined"){
				$("#savebtn").attr("disabled",true);
				$("#genbtn").attr("disabled",true);
				$.fn.zTree.init($("#menup"), setting, result.community);
				$('#myModal').modal('hide');
			}else{
				$("#savebtn").attr("disabled",false);
				$("#genbtn").attr("disabled",false);
				$('#myModal').modal('hide');
			}
	});
});
